package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.po.ScmDocument;
import com.xinsoft.entity.po.SysFileHis;
import com.xinsoft.mapper.ScmDocumentMapper;
import com.xinsoft.service.ScmDocumentService;
import com.xinsoft.service.SysFileHisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZYM
 * @description 针对表【scm_document(文档关联表)】的数据库操作Service实现
 * @createDate 2023-07-18 09:51:58
 */
@Service
public class ScmDocumentServiceImpl extends ServiceImpl<ScmDocumentMapper, ScmDocument>
        implements ScmDocumentService {

    @Autowired
    private SysFileHisService fileHisService;

    @Override
    public List<SysFileHis> getFileHisListByScmId(Integer scmId,Integer scmType) {
        List<SysFileHis> fileHisList = new ArrayList<>();
        List<ScmDocument> documentList = this.list(Wrappers.<ScmDocument>lambdaQuery()
                .eq(ScmDocument::getScmId, scmId)
                .eq(ScmDocument::getScmType,scmType));
        List<Integer> documentIds = documentList.stream().map(ScmDocument::getDocumentId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(documentIds)) {
            fileHisList = fileHisService.listByIds(documentIds);
        }
        return fileHisList;
    }

    @Override
    public void saveDocument(List<SysFileHis> fileHisList, Integer scmId, Integer scmType) {
        List<ScmDocument> documentList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(fileHisList)) {
            fileHisList.forEach(fileHis -> {
                ScmDocument document = new ScmDocument();
                document.setScmId(scmId);
                document.setDocumentId(fileHis.getId());
                document.setScmType(scmType);
                documentList.add(document);
            });
            this.saveBatch(documentList);
        }
    }
}




